const Response = require('../utils/response');
const draftService = require('../services/draftService');

class DraftController {
  // 获取草稿列表
  async getDrafts(req, res) {
    try {
      const drafts = await draftService.getDrafts(req.user.id);
      res.json(Response.success(drafts));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 创建草稿
  async createDraft(req, res) {
    try {
      const draftId = await draftService.createDraft(req.user.id, req.body);
      res.json(Response.success({ id: draftId }, '创建成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 获取草稿详情
  async getDraftById(req, res) {
    try {
      const draft = await draftService.getDraftById(req.params.id, req.user.id);
      res.json(Response.success(draft));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 更新草稿
  async updateDraft(req, res) {
    try {
      await draftService.updateDraft(req.params.id, req.user.id, req.body);
      res.json(Response.success(null, '更新成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 删除草稿
  async deleteDraft(req, res) {
    try {
      await draftService.deleteDraft(req.params.id, req.user.id);
      res.json(Response.success(null, '删除成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }

  // 发布草稿
  async publishDraft(req, res) {
    try {
      const bookId = await draftService.publishDraft(req.params.id, req.user.id);
      res.json(Response.success({ id: bookId }, '发布成功'));
    } catch (error) {
      res.json(Response.error(error.message));
    }
  }
}

module.exports = new DraftController(); 